home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6189 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.0 KB

  1. Path: news.halcyon.com!usenet
  2. From: normanb@halcyon.com (Norm Bryar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Mod or if?
  5. Date: Sat, 10 Feb 1996 16:51:25 GMT
  6. Organization: Northwest Nexus Inc.
  7. Message-ID: <4fiidc$crp@news.halcyon.com>
  8. References: <4fg3jm$fb8@gondor.sdsu.edu>
  9. NNTP-Posting-Host: blv-pm3-ip26.halcyon.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. weikel@rohan.sdsu.edu (weikel) wrote:
  13.  
  14. >Which is better?
  15.  
  16. >x = (x mod 7);
  17.  
  18. >or
  19.  
  20. >if (x == 7) x = 0; 
  21.  
  22. >in terms of performance?  I know that the mod function is slow
  23. >,but the if method seems brute-forceish.
  24.  
  25. You can profile both, I suppose.  Generating asm code may not be too
  26. telling. 
  27.  
  28. But first where is your app getting x?  Is x a local int whose value
  29. is only incremented or decremented by one?  Is it a parameter passed
  30. to your function by possibly dopey users?  If other data in the
  31. procedure is bad, could x ever be incremented by >1 and thus skip
  32. right over seven?  
  33.  
  34. You may find if( x >=7 ) x = 0;  is safer in one case and x=(x mod 7)
  35. better in another.  
  36.  
  37.